#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UTable.Input;
using UTable.Input.MultiTouch;
using UTable.Objects;
using UTable.Objects.Controls;
using System.Collections.ObjectModel;
using System.IO;

namespace UTable.Objects
{
	/// <summary>
	/// Interaction logic for UAlbum.xaml
	/// </summary>
	public partial class UAlbum : UObject
	{
		public UAlbum()
		{
			InitializeComponent();
			this.DataContext = this;
			PhotoPathList = new ObservableCollection<string>();
			GridViewer.ItemTapped += new UGridViewEventHandler(GridViewer_ItemTapped);
		}

		void GridViewer_ItemTapped(UGridViewPanel sender, UGridViewEventArgs args)
		{

		}

		private static void OnFolderPathChanged(DependencyObject obj, DependencyPropertyChangedEventArgs pty)
		{
			(obj as UAlbum).OnFolderPathChanged();
		}

		private void OnFolderPathChanged()
		{
			PhotoPathList.Clear();
			String[] files = Directory.GetFiles(FolderPath);
			List<String> fileList = new List<string>(files);
			fileList.Sort();
			foreach (String file in fileList)
			{
				if (File.Exists(file))
				{
					PhotoPathList.Add(Path.GetFullPath(file));
				}
			}
		}

		private static void OnBrowseModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs pty)
		{
			(obj as UAlbum).OnBrowseModeChanged();
		}

		private void OnBrowseModeChanged()
		{
			switch (BrowseMode)
			{
				case AlbumBrowseMode.Scan:
					ScanViewer.Visibility = Visibility.Visible;
					GridViewer.Visibility = Visibility.Hidden;
					break;
				case AlbumBrowseMode.Grid:
					ScanViewer.Visibility = Visibility.Hidden;
					GridViewer.Visibility = Visibility.Visible;
					break;
			}
		}

		public override void OnMessageReceived(object message)
		{
			base.OnMessageReceived(message);
			if (message is SetFolderPathMessage)
			{
				this.FolderPath = (message as SetFolderPathMessage).FolderPath;
			}
			else if (message is ChangeAlbumBrowseModeMessage)
			{
				this.BrowseMode = (message as ChangeAlbumBrowseModeMessage).BrowseMode;
			}
		}

		public override object OnDataQuerying(object dataFlag)
		{
			if (dataFlag is QueryFolderPathMessage)
			{
				return this.FolderPath;
			}

			return base.OnDataQuerying(dataFlag);
		}

		public String FolderPath
		{
			get { return (String)GetValue(FolderPathProperty); }
			set { SetValue(FolderPathProperty, value); }
		}

		public static readonly DependencyProperty FolderPathProperty =
			DependencyProperty.Register("FolderPath", typeof(String), typeof(UAlbum), new UIPropertyMetadata(String.Empty, new PropertyChangedCallback(OnFolderPathChanged)));

		public AlbumBrowseMode BrowseMode
		{
			get { return (AlbumBrowseMode)GetValue(BrowseModeProperty); }
			set { SetValue(BrowseModeProperty, value); }
		}

		public static readonly DependencyProperty BrowseModeProperty =
			DependencyProperty.Register("BrowseMode", typeof(AlbumBrowseMode), typeof(UAlbum), 
			new UIPropertyMetadata(AlbumBrowseMode.Scan, new PropertyChangedCallback(OnBrowseModeChanged)));

		public ObservableCollection<String> PhotoPathList { get; set; }
	}

	public enum AlbumBrowseMode
	{
		Scan,
		Grid,
	}

	public class ChangeAlbumBrowseModeMessage
	{
		public ChangeAlbumBrowseModeMessage(AlbumBrowseMode mode)
		{
			BrowseMode = mode;
		}

		public AlbumBrowseMode BrowseMode { get; protected set; }
	}
    public class UAlbumModel
    {
        public UAlbumModel Clone()
        {
            return new UAlbumModel(this);
        }

        public UAlbumModel(String folder, AlbumBrowseMode mode)
        {
            this.FolderPath = folder;
            this.BrowseMode = mode;
        }

        public UAlbumModel(UAlbumModel model)
        {
            this.FolderPath = model.FolderPath;
            this.BrowseMode = model.BrowseMode;
        }

        public string FolderPath { get; set; }

        public AlbumBrowseMode BrowseMode { get; set; }
    }
}
